package io.karim.materialtabs.sample;

import io.karim.MaterialTabs;
import io.karim.materialtabs.sample.widget.ActionBar;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.AttrHelper;
import ohos.agp.components.Component;
import ohos.agp.components.PageSlider;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;

/**
 * TabsAbility
 *
 * @since 2021/07/23
 */
public class TabsAbility extends Ability {
    MaterialTabs mMaterialTabs;
    PageSlider mViewPager;
    private ActionBar actionBar;

    @Override
    public void onStart(Intent intent) {
        int color = getColor(ResourceTable.Color_mantis);
        initStatus(this, color);
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_tabs);
        initView();
    }

    private void initView() {
        mMaterialTabs = (MaterialTabs) findComponentById(ResourceTable.Id_material_tabs);
        mViewPager = (PageSlider) findComponentById(ResourceTable.Id_view_pager);
        actionBar = (ActionBar) findComponentById(ResourceTable.Id_actionbar);
        int numberOfTabs = 3;
        if (getIntent() != null) {
            numberOfTabs = getIntent().getIntParam(TabsSettingsFragment.NUMBER_OF_TABS, 3);
        }
        SamplePagerAdapter adapter = new SamplePagerAdapter(numberOfTabs);
        mViewPager.setProvider(adapter);
        mMaterialTabs.setViewPager(mViewPager);
        mMaterialTabs.setOnTabSelectedListener(position -> {
        });

        mMaterialTabs.setOnTabReselectedListener(position -> {
        });

        applyParametersFromIntentintent();

        mViewPager.setPageMargin(AttrHelper.vp2px(4, getContext()));
        mViewPager.setCurrentPage(0);
        actionBar.addClickListener((clickType, position) -> {
            if (clickType == ActionBar.ActionBarType.LEFT_ICON) {
                onBackPressed();
            }
        });
    }

    private void applyParametersFromIntentintent() {
        Intent intent = getIntent();
        if (intent != null) {
            int showToolbar = intent.getBooleanParam(TabsSettingsFragment.SHOW_TOOLBAR, false) ? Component.VISIBLE : Component.HIDE;
            int indicatorColor = getColor(intent.getIntParam(TabsSettingsFragment.INDICATOR_COLOR, Color.WHITE.getValue()));
            int underlineColor = getColor(intent.getIntParam(TabsSettingsFragment.UNDERLINE_COLOR, Color.WHITE.getValue()));
            int indicatorHeightDp = intent.getIntParam(TabsSettingsFragment.INDICATOR_HEIGHT, 0);
            int underlineHeightDp = intent.getIntParam(TabsSettingsFragment.UNDERLINE_HEIGHT, 0);
            int tabPaddingDp = intent.getIntParam(TabsSettingsFragment.TAB_PADDING, 0);
            actionBar.setVisibility(showToolbar);
            mMaterialTabs.setIndicatorColor(indicatorColor);
            mMaterialTabs.setUnderlineColor(underlineColor);
            mMaterialTabs.setIndicatorHeight(AttrHelper.vp2px(indicatorHeightDp, getContext()));
            mMaterialTabs.setUnderlineHeight(AttrHelper.vp2px(underlineHeightDp, getContext()));
            mMaterialTabs.setTabPaddingLeftRight(AttrHelper.vp2px(tabPaddingDp, getContext()));

            boolean paddingMiddle = intent.getBooleanParam(TabsSettingsFragment.PADDING_MIDDLE, false);
            boolean sameWeightTabs = intent.getBooleanParam(TabsSettingsFragment.SAME_WEIGHT_TABS, false);
            boolean textAllCaps = intent.getBooleanParam(TabsSettingsFragment.TEXT_ALL_CAPS, false);

            mMaterialTabs.setPaddingMiddle(paddingMiddle);
            mMaterialTabs.setSameWeightTabs(sameWeightTabs);
            mMaterialTabs.setAllCaps(textAllCaps);
            setColor(intent);
        }
    }

    private void setColor(Intent intent) {
        int toolbarColor = getColor(intent.getIntParam(TabsSettingsFragment.TOOLBAR_BACKGROUND, Color.BLACK.getValue()));
        int tabBackgroundColor = getColor(intent.getIntParam(TabsSettingsFragment.TAB_BACKGROUND, -1));
        ShapeElement shapeElement = new ShapeElement();
        shapeElement.setRgbColor(RgbColor.fromArgbInt(tabBackgroundColor));
        mMaterialTabs.setBackground(shapeElement);
        ShapeElement actShape = new ShapeElement();
        actShape.setRgbColor(RgbColor.fromArgbInt(toolbarColor));
        actionBar.setBackground(actShape);
        initStatus(this, toolbarColor);

        int textColorSelected = getColor(intent.getIntParam(TabsSettingsFragment.TEXT_COLOR_SELECTED, Color.WHITE.getValue()));
        int textColorUnselected = getColor(intent.getIntParam(TabsSettingsFragment.TEXT_COLOR_UNSELECTED, Color.WHITE.getValue()));

        mMaterialTabs.setTextColorSelected(textColorSelected);
        mMaterialTabs.setTextColorUnselected(textColorUnselected);

        int rippleDuration = intent.getIntParam(RippleSettingsFragment.RIPPLE_DURATION, 0);
        float rippleAlphaFloat = intent.getFloatParam(RippleSettingsFragment.RIPPLE_ALPHA_FLOAT, 0);
        int rippleColor = getColor(intent.getIntParam(RippleSettingsFragment.RIPPLE_COLOR, Color.BLACK.getValue()));
        boolean rippleDelayClick = intent.getBooleanParam(RippleSettingsFragment.RIPPLE_DELAY_CLICK, false);
        float rippleDiameterDp = intent.getFloatParam(RippleSettingsFragment.RIPPLE_DIAMETER, 0);
        int rippleFadeDuration = intent.getIntParam(RippleSettingsFragment.RIPPLE_FADE_DURATION, 0);
        int rippleHighlightColor = getColor(intent.getIntParam(RippleSettingsFragment.RIPPLE_HIGHLIGHT_COLOR, Color.BLACK.getValue()));
        boolean rippleOverlay = intent.getBooleanParam(RippleSettingsFragment.RIPPLE_OVERLAY, false);
        boolean ripplePersistent = intent.getBooleanParam(RippleSettingsFragment.RIPPLE_PERSISTENT, false);
        int rippleRoundedCornusRadiusDp = intent.getIntParam(RippleSettingsFragment.RIPPLE_ROUNDED_CORNERS_RADIUS, 0);

        mMaterialTabs.setRippleDuration(rippleDuration);
        mMaterialTabs.setRippleAlphaFloat(rippleAlphaFloat);
        mMaterialTabs.setRippleColor(rippleColor);
        mMaterialTabs.setRippleDelayClick(rippleDelayClick);
        mMaterialTabs.setRippleDiameterDp(rippleDiameterDp);
        mMaterialTabs.setRippleFadeDuration(rippleFadeDuration);
        mMaterialTabs.setRippleHighlightColor(rippleHighlightColor);
        mMaterialTabs.setRippleInAdapter(false);
        mMaterialTabs.setRippleOverlay(rippleOverlay);
        mMaterialTabs.setRipplePersistent(ripplePersistent);
        mMaterialTabs.setRippleRoundedCornersDp(rippleRoundedCornusRadiusDp);
    }


    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    /**
     * 状态栏
     *
     * @param ability ability
     * @param color   color
     */
    public static void initStatus(Ability ability, int color) {
        ability.getWindow().setStatusBarColor(color);

    }
}
